<?php
use App\Base\Annotation\Common;
/**
 * @param $class
 * @param $propertyName
 * @return Common|object|null
 */
function getAnnotationData($class,$propertyName)
{
    try {

        $reClass = new \ReflectionClass($class);
        $property = $reClass->getProperty($propertyName);
        $reader = new \Doctrine\Common\Annotations\AnnotationReader();
        return $reader->getPropertyAnnotation($property,"App\Base\Annotation\Common");
    } catch (Exception $exception) {
        return null;
    }
}
function getEntityAnnotationData($class){
    try {
        $re = [];
        $reClass = new \ReflectionClass($class);
        foreach ($reClass->getProperties() as $property) {
            $re[$property->getName()] = getAnnotationData($class,$property->getName());
        }
        return $re;
    } catch (Exception $exception) {
        return [];
    }
}

//筛选条件可以用形式
//1:_where[name]=1
//2:_where[eq][name]=1
//3:_where[gt][name]=1
//4:_where[like][name]=1
//5:_where[in][name]=[1,2,3]
function createWhere4MongoDB($array){
    if (!$array) {
        return [];
    }
    $re = [];
    foreach ($array as $expression => $value) {
        if (is_array($value)) {
            foreach ($value as $key=>$dataVal) {
                if ($dataVal === "") {
                    continue;
                }
                if ($expression == "like") {
                    $expression = "regex";
                }
                if ($expression == "in") {
                    if (!is_array($dataVal)) {
                        $tmp = json_decode($dataVal,true);
                        if (!is_array($tmp)) {
                            $tmp = myExplodeNumbers($dataVal);
                            if (!is_array($tmp)) {
                                $dataVal = [$dataVal];
                            }else{
                                $dataVal = $tmp;
                            }
                        }else{
                            $dataVal = $tmp;
                        }
                    }
                }
                if ($expression == "gt" ||$expression == "gte" ||$expression == "lt" ||$expression == "lte" ) {
                    $dataVal *=1;
                }

                //猜测时间类型---->要求以At结尾
                if (substr($key,-2) === "At") {
                    //todo 更严格的检查应该正则匹配值检测是不是日期----懒，不做了
                    $dataVal = new DateTime($dataVal);
                }
                $re[$key]["$".$expression]=$dataVal;
            }
        }else{
            if ($value === "") {
                continue;
            }


            if ($expression == "gt" ||$expression == "gte" ||$expression == "lt" ||$expression == "lte" ) {
                $value *=1;
            }
            $re[$expression] = $value;
        }
    }
    return $re;

}

function createSearch4MongoDB($keywords,array $fields = [],$orOrAnd = 'or')
{
    $re=[];
    if (!$keywords || !$fields) {
        return [];
    }
    foreach ($fields as $field) {
        $re[] = [$field=>['$regex'=>"{$keywords}"]];
    }
    return [['$'.$orOrAnd=>$re]];
}